package com.time.company.app;

import android.app.Activity;

import java.util.LinkedList;
import java.util.List;

/**
 *
 * Created by Bing on 2016/8/23.
 * E-mail is tx16bing@163.com
 */
public class MyManager {

    private volatile static MyManager instance = null;
    private static List<Activity> mActivites = new LinkedList<Activity>();

    public static MyManager getInstance(){
        if (instance == null){
            synchronized (MyManager.class){
                if (instance == null){
                    instance = new MyManager();
                }
            }
        }
        return instance;
    }

    /**
     * return mActivites size
     * @return
     */
    public int size(){
        return mActivites.size();
    }

    /**
     * get forward activity
     * @return
     */
    public synchronized Activity getForwardActivity(){
        return size() > 0 ? mActivites.get(size() - 1) : null;
    }

    /**
     * add activity
     * @param activity
     */
    public synchronized void addActivity(Activity activity){
        mActivites.add(activity);
    }

    /**
     * remove activity
     * @param activity
     */
    public synchronized void removeActivity(Activity activity){
        if (mActivites.contains(activity)){
            mActivites.remove(activity);
        }
    }

    public synchronized void clear(){
        int activitySize = mActivites.size();
        for (int i = activitySize - 1; i > -1; i--){

            Activity activity = mActivites.get(i);
            removeActivity(activity);
            activity.finish();
        }
    }

    public synchronized void clearTop(){
        int activitySize = mActivites.size();
        for (int i = activitySize - 2; i > -1; i--){
            Activity activity = mActivites.get(i);
            removeActivity(activity);
            activity.finish();
        }
    }

}
